package org.mics.cyy.backUser.controller;

import java.util.List;

import javax.annotation.Resource;

import org.mics.core.page.PageQuery;
import org.mics.core.response.MultipleDataResponse;
import org.mics.core.response.OneDataResponse;
import org.mics.core.response.PageDataResponse;
import org.mics.core.response.SimpleResponse;
import org.mics.cyy.backUser.entity.MenuDO;
import org.mics.cyy.backUser.request.MenuRequest;
import org.mics.cyy.backUser.service.MenuService;
import org.mics.cyy.backUser.vo.MenuVO;
import org.mics.jpa.helper.TreeUtils;
import org.mics.lang.bean.BeanConvertUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 *菜单
 * @author mics
 * @date 2020年6月10日
 * @version  1.0
 */
@RestController
@RequestMapping("/menu")
@Tag(name = "菜单")
public class MenuController {
	@Resource
	private MenuService menuService;
	
	@Operation(summary =  "增加/修改菜单")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
	public SimpleResponse save(@Validated @RequestBody MenuRequest menuRequest ) {
		String id =   menuService.add(menuRequest);
		return new OneDataResponse<String>(id);
	}
	
	@Operation(summary =  "删除菜单")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
	public SimpleResponse delete(@PathVariable("id") String id) {
		menuService.delete(id);
        return new SimpleResponse();
	}
	
	@Operation(summary =  "菜单分页列表")
    @RequestMapping(value = "/listByPage",method = RequestMethod.POST)
	public PageDataResponse<MenuVO> listByPage(@RequestBody PageQuery pageQuery) {
		PageDataResponse<MenuVO> pageDataResponse =   menuService.listByPage(pageQuery);
		return pageDataResponse;
	}
	
	@Operation(summary =  "菜单列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
	public MultipleDataResponse<MenuVO> list(@RequestParam(value = "name",required = false)String name) {
		List<MenuDO> menus =   menuService.list(name);
		return new MultipleDataResponse<MenuVO>(BeanConvertUtil.convertList(menus, MenuVO.class));
	}
	
	@Operation(summary =  "菜单树结构")
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
	public MultipleDataResponse<MenuVO> tree() {
		List<MenuDO> menus =   menuService.list(null);
		List<MenuVO> menuVOs = BeanConvertUtil.convertList(menus, MenuVO.class);
		return new MultipleDataResponse<MenuVO>(TreeUtils.convertMulTree(menuVOs)) ;
	}
	
	@Operation(summary =  "根据上级菜单查询下级菜单列表")
    @RequestMapping(value = "/listByParentId/{parentId}",method = RequestMethod.GET)
	public MultipleDataResponse<MenuVO> listByParentId(@PathVariable("parentId")String parentId) {
		List<MenuDO> menuDOs =   menuService.findByParentId(parentId);
		return new MultipleDataResponse<MenuVO>(BeanConvertUtil.convertList(menuDOs, MenuVO.class));
	}
	
	@Operation(summary =  "根据角色id查询菜单")
    @RequestMapping(value = "/findByRoleId/{roleId}",method = RequestMethod.GET)
	public MultipleDataResponse<MenuVO> findByRoleId(@PathVariable("roleId") String roleId ) {
		List<MenuDO> menuDOs =   menuService.findByRoleId(roleId);
		List<MenuVO> menuVOs = BeanConvertUtil.convertList(menuDOs, MenuVO.class);
		return new MultipleDataResponse<MenuVO>(TreeUtils.convertMulTree(menuVOs));
	}
}
